<?php
/*
function connecttodb()
function isExistedUsername()
function addProvider($username,$password,$fname,$lname,$company,$chIdArray)
function addReader($username,$password,$fname,$lname,$company,$rank)
function addChannel($title,$link,$description,$pubdate,$language,$lastbuilddate,$docs,$generator
	,$managineditor,$webmaster,$ttl,$rank)

////////////// EDIT //////////////////
function editProvider($username,$password,$fname,$lname,$company,$chIdArray)
function editReader($username,$password,$fname,$lname,$company,$rank)
function editChannel($id,$title,$link,$description,$pubdate,$language,$lastbuilddate,$docs,$generator
	,$managineditor,$webmaster,$ttl,$rank)

////////////// getALL //////////////////
function getAllProvider()
function getAllReader()
function getAllChannel()

////////////// SEARCH //////////////////
function searchProvider($user,$fname,$lname,$company)
function searchReader($user,$fname,$lname,$company)
function searchChannel($id,$title,$link,$description,$pubdate,$language,$lastbuilddate,$docs
	,$generator,$managineditor,$webmaster,$ttl,$rank)
function searchProviderChannel($username)

/////////////DELETE////////////////////
*/

/*function connecttodb()
{
	$serverName="localhost";
	$dbname="it_feed";
	$dbuser="root";
	$dbpassword="mysql";
	global $link;
	$link=mysql_connect ("$serverName","$dbuser","$dbpassword");
	if(!$link){die("Could not connect to MySQL");}
	mysql_select_db("$dbname",$link) or die ();
}*/
include("connect.inc.php");

function isExistedUsername($username) {

	$result = mysql_query("SELECT username FROM provider where username='$username'");
	$tmp = mysql_num_rows($result)==1;
	
	$result = mysql_query("SELECT username FROM reader where username='$username'");
	$tmp = $tmp | mysql_num_rows($result)==1;
	
	return $tmp;
}

////////////// ADD //////////////////

function addProvider($username,$password,$fname,$lname,$company,$chIdArray){
	$password=sha1($password);
	
	$sql = "LOCK TABLES provider WRITE, providerchannel WRITE";
	mysql_query($sql);
	
	$sql="insert into provider (username,password,fname,lname,company) 
		value('$username','$password','$fname','$lname','$company')";
	mysql_query($sql) or die();
	
	if($chIdArray == '')return;
	foreach($chIdArray as $key => $value){
		$sql="insert into providerchannel (username,channelid) value('$username','$value')";
		mysql_query($sql) or die();
	}
	
	$sql = "UNLOCK TABLES";
	mysql_query($sql);

}

function addPublisher($username,$password,$fname,$lname,$company,$chIdArray){
	$password=sha1($password);
	
	$sql = "LOCK TABLES publisher WRITE, publisherchannel WRITE";
	mysql_query($sql);
	
	$sql="insert into publisher (username,password,fname,lname,company) 
		value('$username','$password','$fname','$lname','$company')";
	mysql_query($sql) or die();
	
	if($chIdArray == '')return;
	foreach($chIdArray as $key => $value){
		$sql="insert into publisherchannel (username,channelid) value('$username','$value')";
		mysql_query($sql) or die();
	}
	
	$sql = "UNLOCK TABLES";
	mysql_query($sql);

}

function addReader($username,$password,$fname,$lname,$company,$rank){
	$password=sha1($password);
	
	$sql = "LOCK TABLES reader WRITE";
	mysql_query($sql);
	
	$sql = "insert into reader (username,password,fname,lname,company,rank) 
		value('$username','$password','$fname','$lname','$company','$rank')";
	mysql_query($sql) or die();
	
	$sql = "UNLOCK TABLES";
	mysql_query($sql);

}

function addChannel($title,$link,$description,$pubdate = '',$language,$lastbuilddate = '',$docs,$generator
	,$managineditor,$webmaster,$ttl,$rank){
	
	global $db;
	
	$sql = "LOCK TABLES channel WRITE";
	$db->sql_query($sql);
	
	$sql = "INSERT INTO channel 
		(title,link,description,pubdate,language,lastbuilddate,docs,generator
			,managineditor,webmaster,ttl,rank) 
		VALUE('$title','$link','$description', NOW(),'$language', NOW(),'$docs','$generator'
			,'$managineditor','$webmaster','$ttl','$rank')";
	$db->sql_query($sql);
	
	$sql = "SELECT id FROM channel WHERE title = '$title' AND description = '$description'";
	$query = $db->sql_query($sql);
	$row = $db->sql_fetchrow($query);
	
	$rss_link = "http://".$_SERVER['HTTP_HOST'].substr($_SERVER['SCRIPT_NAME'], 0, strrpos($_SERVER['SCRIPT_NAME'], "/"))."/rss.php?channel_id=".$row['id'];
	$sql = "UPDATE channel SET docs = '$rss_link' WHERE id = '" . $row['id'] . "'";
	$db->sql_query($sql);
	
	$sql = "UNLOCK TABLES";
	$db->sql_query($sql);
}


////////////// EDIT //////////////////

function editProvider($username,$password,$fname,$lname,$company,$chIdArray){
	
	$sql = "LOCK TABLES provider WRITE, providerchannel WRITE";
	mysql_query($sql);
	
	if($password!=null||$fname!=null||$lname!=null||$company!=null){
		$sql="update provider set ";
		if($password!=null) $sql.="password='".sha1($password)."',";
		if($fname!=null) $sql.="fname='$fname',";
		if($lname!=null) $sql.="lname='$lname',";
		if($company!=null) $sql.="company='$company',";
		$sql=substr($sql,0,strlen($sql)-1);
		$sql.= " where username='$username'";
		//echo $sql;
		mysql_query($sql);
	}
	
	//if($chIdArray!=null){
		mysql_query("delete from providerchannel where username='$username'");
		foreach($chIdArray as $value){
		$sql="insert into providerchannel (username,channelid) value('$username','$value')";
		mysql_query($sql) or die();
		}
	//}
	
	$sql = "UNLOCK TABLES";
	mysql_query($sql);

}

function editPublisher($username,$password,$fname,$lname,$company,$chIdArray) {

	global $db;
	
	$sql = "LOCK TABLES publisher WRITE, publisherchannel WRITE";
	$db->sql_query($sql);
	
	if($password!=null||$fname!=null||$lname!=null||$company!=null){
		$sql="update publisher set ";
		if($password!=null) $sql.="password='".sha1($password)."',";
		if($fname!=null) $sql.="fname='$fname',";
		if($lname!=null) $sql.="lname='$lname',";
		if($company!=null) $sql.="company='$company',";
		$sql=substr($sql,0,strlen($sql)-1);
		$sql.= " where username='$username'";
		//echo $sql;
		$db->sql_query($sql);
	}
	
	//if($chIdArray!=null){
		mysql_query("delete from publisherchannel where username='$username'");
		foreach($chIdArray as $value){
			$sql="insert into publisherchannel (username,channelid) value('$username','$value')";
			$db->sql_query($sql);
		}
	//}

	$sql = "UNLOCK TABLES";
	$db->sql_query($sql);
}

function editReader($username,$password,$fname,$lname,$company,$rank){

	global $db;
	
	$sql = "LOCK TABLES reader WRITE";
	$db->sql_query($sql);
	
	if($password!=null||$fname!=null||$lname!=null||$company!=null){
		$sql="update reader set ";
		if($password!=null){ $sql.="password='".sha1($password)."',";}
		if($fname!=null) $sql.="fname='$fname',";
		if($lname!=null) $sql.="lname='$lname',";
		if($company!=null) $sql.="company='$company',";
		if($rank!=null) $sql.="rank='$rank',";
		$sql=substr($sql,0,strlen($sql)-1);
		$sql.= " where username='$username'";
		//echo $sql;
		$db->sql_query($sql);
	}
	
	$sql = "UNLOCK TABLES";
	$db->sql_query($sql);
}

function editChannel($id,$title,$link,$description,$pubdate,$language,$lastbuilddate,$docs,$generator
	,$managineditor,$webmaster,$ttl,$rank){

	global $db;
	
	$sql = "LOCK TABLES channel WRITE";
	$db->sql_query($sql);
	
	if($title!=null||$link!=null||$description!=null||$pubdate!=null
		||$language!=null||$lastbuilddate!=null||$docs!=null||$generator!=null
		||$managineditor!=null||$webmaster!=null||$ttl!=null||$rank!=null){
		$sql="update channel set ";
		if($title!=null) $sql.="title='$title',";
		if($link!=null) $sql.="link='$link',";
		if($description!=null) $sql.="description='$description',";
		if($pubdate!=null) $sql.="pubdate='$pubdate',";
		if($language!=null) $sql.="language='$language',";
		if($lastbuilddate!=null) $sql.="lastbuilddate='$lastbuilddate',";
		if($docs!=null) $sql.="docs='$docs',";
		if($generator!=null) $sql.="generator='$generator',";
		if($managineditor!=null) $sql.="managineditor='$managineditor',";
		if($webmaster!=null) $sql.="webmaster='$webmaster',";
		if($ttl!=null) $sql.="ttl='$ttl',";
		if($rank!=null) $sql.="rank='$rank',";
		$sql=substr($sql,0,strlen($sql)-1);
		$sql.= " where id='$id'";
		$db->sql_query($sql);
	}

	$sql = "UNLOCK TABLES";
	$db->sql_query($sql);
}


////////////// getALL //////////////////

function getAllProvider(){
	$result=mysql_query("select * from provider");
	while($row[] = mysql_fetch_assoc($result));
	return $row;
}

function getAllPublisher(){
	$result=mysql_query("select * from publisher");
	while($row[] = mysql_fetch_assoc($result));
	return $row;
}

function getAllReader(){
	$result=mysql_query("select * from reader");
	while($row[] = mysql_fetch_assoc($result));
	return $row;
}

function getAllChannel(){
	$result=mysql_query("SELECT * FROM channel WHERE docs NOT IN (SELECT link FROM externalfeed GROUP BY link)");
	while($r= mysql_fetch_assoc($result)){$row[] = $r;}
	return $row;
}


////////////// SEARCH //////////////////

function searchProvider($username,$fname,$lname,$company){
	$sql="select * from provider where ";
	$tmp=false;
	$arg_list = func_get_args();
	$argname=array("username","fname","lname","company");
	foreach($arg_list as $key=>$value){
		if($value!=null){
			$sql.=" $argname[$key] like '%$value%' and";
			$tmp=true;
		}
	}
	$sql=substr($sql,0,strlen($sql)-3);
	if($tmp) $result=mysql_query($sql) or die();
	else $result=mysql_query("select * from provider") or die();
	while($row[]=mysql_fetch_assoc($result));
	return $row;
}

function searchPublisher($username,$fname,$lname,$company){
	$sql="select * from publisher where ";
	$tmp=false;
	$arg_list = func_get_args();
	$argname=array("username","fname","lname","company");
	foreach($arg_list as $key=>$value){
		if($value!=null){
			$sql.=" $argname[$key] like '%$value%' and";
			$tmp=true;
		}
	}
	$sql=substr($sql,0,strlen($sql)-3);
	if($tmp) $result=mysql_query($sql) or die();
	else $result=mysql_query("select * from publisher") or die();
	while($row[]=mysql_fetch_assoc($result));
	return $row;
}

function searchReader($username,$fname,$lname,$company){
	$sql="select * from reader where ";
	$tmp=false;
	$arg_list = func_get_args();
	$argname=array("username","fname","lname","company");
	foreach($arg_list as $key=>$value){
		if($value!=null){
			$sql.=" $argname[$key] like '%$value%' and";
			$tmp=true;
		}
	}
	$sql=substr($sql,0,strlen($sql)-3);
	
	//echo $sql;
	if($tmp) $result=mysql_query($sql) or die();
	else $result=mysql_query("select * from reader") or die();
	while($row[]=mysql_fetch_assoc($result));
	return $row;
}

function searchChannel($id,$title,$link,$description,$pubdate,$language,$lastbuilddate,$docs,$generator
	,$managineditor,$webmaster,$ttl,$rank){
	$sql="select * from channel where ";
	$tmp=false;
	$arg_list = func_get_args();
	$argname=array("id","title","link","description","pubdate",
	"language","lastbuilddate","docs","generator"
	,"managineditor","webmaster","ttl","rank");
	foreach($arg_list as $key=>$value){
		if($argname[$key] != ''){
			
		}
	}
	foreach($arg_list as $key=>$value){
		if($value!=null){
			$sql.=" $argname[$key] like '%$value%' and";
			$tmp=true;
		}
	}
	$sql=substr($sql,0,strlen($sql)-3);
	//echo $sql;
	if($tmp) $result=mysql_query($sql);
	else $result=mysql_query($sql);
	while($row[]=mysql_fetch_assoc($result));
	return $row;
}

function searchProviderChannel($username){
	if($username == null)
		return null;
	
	$sql="select * from providerchannel where username='".$username."'";
	//echo $sql;
	$result=mysql_query($sql);
	while($r=mysql_fetch_assoc($result)){$row[]=$r;}
	return $row;
}

function searchPublisherChannel($username){
	if($username == null)
		return null;
	
	$sql="select * from publisherchannel where username='".$username."'";
	$result=mysql_query($sql);
	while($r=mysql_fetch_assoc($result)){$row[]=$r;}
	return $row;
}

function deleteChannel($cid){
	if($cid == null)
		return "Channel id not found.";
	
	$sql = "LOCK TABLES channel WRITE";
	mysql_query($sql);
	
	$sql="delete from channel where id='$cid'";
	$result=mysql_query($sql);
	while($r=mysql_fetch_assoc($result)){$row[]=$r;}
	
	$sql = "UNLOCK TABLES";
	mysql_query($sql);
	
	return $row;
}//////////// get ///////////////////function getProvider($username){	$sql="select * from provider where username='$username' ";	$result=mysql_query($sql) or die();	$row=mysql_fetch_assoc($result);	return $row;}function getPublisher($username){	$sql="select * from publisher where username='$username'";	$result=mysql_query($sql) or die();	$row=mysql_fetch_assoc($result);	return $row;}function getReader($username,$fname,$lname,$company){	$sql="select * from reader where username='$username' ";	$result=mysql_query($sql) or die();	$row=mysql_fetch_assoc($result);	return $row;}function getChannel($id){	$sql="select * from channel where id='$id'";	$result=mysql_query($sql) or die();	$row=mysql_fetch_assoc($result);	return $row;}

//error_reporting(6143);
/*connecttodb();*/

?> 